<?php

class Ads extends CI_Controller {

    public $_updir = 'public/template/backend/images/banner/';
    private $_updat;
    private $_auth;

    public function __construct() {
        parent::__construct();
        $this->_auth = $this->myclass->checkuser();
        if ($this->_auth == FALSE)
            redirect("backend/home");
    }

    public function index() {        
        $data['data']['listpro'] = $this->mads->fetchAll();
        $data['title'] = "Bảng quảng cáo";
        $data['temp'] = "backend/ads/home";
        $this->load->view("backend/layout/index", $data);
    }

    public function add() {
        if ($this->input->post("add")) {
            $post = $this->input->post("data");
            $this->form_validation->set_error_delimiters('<li>', '</li>');
            $this->form_validation->set_rules('data[url]', 'Đường dẫn', 'trim|required');
            $this->form_validation->set_rules('data[date_start]', 'Ngày bắt đầu', 'trim|required');
            $this->form_validation->set_rules('data[date_end]', 'Ngày kết thúc', 'trim|required');            
            $config['upload_path'] = $this->_updir;
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size'] = '2000';
            $config['overwrite'] = TRUE;
            $this->load->library('upload', $config);
            if ($this->form_validation->run() == TRUE) {
                if ($this->upload->do_upload('img')) {
                    $this->_updat = $this->upload->data();
                    $post['img'] = $this->_updir . $this->_updat['file_name'];                  
                    $this->mads->insert($post);
                    redirect("backend/ads");
                } else {
                    $data['data'] = array('error' => $this->upload->display_errors());
                }
            }
        }
        $data['title'] = "Thêm quảng cáo";
        $data['temp'] = "backend/ads/form";
        $this->load->view("backend/layout/index", $data);
    }

    public function delete($id) {
        $this->mads->delete($id);
        redirect("backend/ads");
    }

    public function deleteall() {
        $this->mads->deleteAll();
        redirect("backend/ads");
    }

    public function update($id) {
        $data['data']['pro']= $this->mads->fetchOne($id);
        
        if ($this->input->post("add")) {
            $post = $this->input->post("data");
        }
        $this->form_validation->set_error_delimiters('<li>', '</li>');
        $this->form_validation->set_rules('data[url]', 'Đường dẫn', 'trim|required');
        $this->form_validation->set_rules('data[date_start]', 'Ngày bắt đầu', 'trim|required');
        $this->form_validation->set_rules('data[date_end]', 'Ngày kết thúc', 'trim|required');

        if ($this->form_validation->run() == TRUE){
            $config['upload_path'] = $this->_updir;
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size'] = '2000';
            $config['overwrite'] = TRUE;
            $this->load->library('upload', $config);
            if ($this->upload->do_upload('img')) {
                $this->_updat = $this->upload->data();
                $post['img'] = $this->_updir . $this->_updat['file_name'];
            } else {
                $post['img'] = $data['data']['pro']->img;
            }           
            $this->mads->update($id,$post);
            redirect("backend/ads");
        }
        
        $data['title'] = "Sửa quảng cáo";
        $data['temp'] = "backend/ads/form";
        $this->load->view("backend/layout/index", $data);
    }

}
